package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.enums.FlowType;
import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.entity.mysql.FlowConfiguration;
import cn.fintecher.pangolin.service.business.model.request.CreateConfigFlowRequest;
import cn.fintecher.pangolin.service.business.model.request.FlowConfigSearchRequest;
import cn.fintecher.pangolin.service.business.model.request.ModifyFlowConfigRequest;
import cn.fintecher.pangolin.service.business.repository.jparepository.FlowConfigurationRepository;
import cn.fintecher.pangolin.service.business.service.BaseService;
import cn.fintecher.pangolin.service.business.service.FlowConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Objects;

/**
 * @Author wangxiangdong
 * @Description 流程配置
 * @Date 2019/9/18 18:58
 **/
@RestController
@RequestMapping("/api/flowConfigurationController")
@Api(value = "审批流程配置", description = "审批流程配置")
public class FlowConfigurationController {

    @Autowired
    private BaseService baseService;

    @Autowired
    private FlowConfigurationService flowConfigurationService;

    @Autowired
    private FlowConfigurationRepository flowConfigurationRepository;

    @ApiOperation(value = "新建配置流程", notes = "新建配置流程")
    @PostMapping("/createFlowConfiguration")
    public ResponseEntity<Void> createFlowConfiguration(@RequestBody CreateConfigFlowRequest request, @RequestHeader(value = "X-UserToken") String token) throws BadRequestException {
        UserModel userByToken = baseService.getUserByToken(token);
        flowConfigurationService.createFlowConfig(request, userByToken);
        if (Objects.isNull(userByToken)) {
            throw new BadRequestException(null, "login", "operator.not.login");
        }
        return ResponseEntity.ok().build();
    }

    @ApiOperation(value = "删除配置流程", notes = "删除配置流程")
    @DeleteMapping("/deleteFlowConfig/{id}")
    public ResponseEntity<Void> deleteFlowConfig(@PathVariable String id) {
        flowConfigurationRepository.deleteById(id);
        return ResponseEntity.ok().build();
    }

    @ApiOperation(value = "配置流程分页查询", notes = "配置流程分页查询")
    @GetMapping("/getAllFlowConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FlowConfiguration>> getAllFlowConfig(@ApiIgnore Pageable pageable, FlowConfigSearchRequest flowConfigSearchRequest) {
        Sort sort = new Sort(Sort.Direction.DESC, "operatorTime");
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        return ResponseEntity.ok().body(flowConfigurationRepository.findAll(flowConfigSearchRequest.generateBooleanBuilder(), pageable));
    }


    @ApiOperation(value = "修改配置流程", notes = "修改配置流程")
    @PostMapping("/modifyFlowConfig")
    public ResponseEntity<Void> modifyFlowConfig(@RequestBody ModifyFlowConfigRequest modifyFlowConfigRequest, @RequestHeader(value = "X-UserToken") String token) {
        UserModel operator = baseService.getUserByToken(token);
        if (Objects.isNull(operator)) {
            throw new BadRequestException(null, "login", "operator.not.login");
        }
        flowConfigurationService.modifyFlowConfig(modifyFlowConfigRequest, operator);
        return ResponseEntity.ok().build();
    }

    @ApiOperation(value = "根据申请类型和审批类型查询唯一流程", notes = "根据申请类型和审批类型查询唯一流程")
    @GetMapping("/findFlowConfigByType")
    public ResponseEntity<FlowConfiguration> findFlowConfigByType(FlowType flowType) {
        return ResponseEntity.ok().body(flowConfigurationService.findFlowConfigByType(flowType));
    }
}
